<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>openlayers学习-3.标记中心点示例</title>
    <link href="./css/ol.css" rel="stylesheet" type="text/css" />
    <script src="./js/ol.js" type="text/javascript"></script>
    <style>
        html,
        body {
            height: 100%;
            width: 100%;
            overflow: hidden;
        }
    </style>
</head>

<body>
    <div id="map" style="height: 100%; width: 100%; overflow: hidden"></div>
</body>
<script>
    // 中心点坐标 ('EPSG:4326', 'EPSG:3857' 坐标系知识参考博客https://www.cnblogs.com/E7868A/p/11460865.html)
    var center_point = ol.proj.transform([121.487899486, 31.24916171], 'EPSG:4326', 'EPSG:3857');
    // 创建地图
    var map = new ol.Map({
        // 让id为map的div作为地图的容器
        target: 'map',
        // 设置地图图层
        layers: [
            // 创建一个使用Open Street Map地图源的瓦片图层
            new ol.layer.Tile({ source: new ol.source.OSM() })
        ],
        // 设置显示地图的视图
        view: new ol.View({
            center: center_point,    // 定义地图显示中心
            zoom: 14            // 定义地图显示层级
        }),
        // 设置地图控件，默认的三个控件都不显示
        controls: ol.control.defaults({
            attribution: false,
            rotate: false,
            zoom: false
        })
    });
    // 新建图层，用于标记中心点
    var locationLayer = new ol.layer.Vector();
    var locationVector = new ol.source.Vector();
    locationLayer.setSource(locationVector);

    // openlayers点对象
    var locationPoint = new ol.geom.Point(center_point);
    var locationFeature = new ol.Feature();
    // 设置几何图形
    locationFeature.setGeometry(locationPoint);
    // 设置图层属性
    locationVector.addFeature(locationFeature);

    // style样式的使用，icon图标
    var centerPointStyle = new ol.style.Style({
        image: new ol.style.Icon({
            src: './images/location.png',
            scale: 0.3,
            anchor: [0.5, 1]
        })
    });
    locationLayer.setStyle(centerPointStyle);
    map.addLayer(locationLayer);

</script>

</html>